package cn.tedu.ximart.server.controller;


import cn.tedu.ximart.server.pojo.dto.AttributeAddNewDTO;
import cn.tedu.ximart.server.pojo.vo.AttributeListItemVO;
import cn.tedu.ximart.server.service.IAttributeService;
import cn.tedu.ximart.server.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "属性管理模块")
@RestController
@RequestMapping("/attributes")
public class AttributeController {

    @Autowired
    IAttributeService attributeService;

    @PostMapping("/add-new")
    @ApiOperation("添加属性")
    @ApiOperationSupport(order = 100)
    @PreAuthorize("hasAnyAuthority('/pms/product/update','/pms/product/read')")
    public JsonResult addNew(@RequestBody AttributeAddNewDTO attributeAddNewDTO){
        log.debug("准备开始处理添加属性业务，接受到的参数为：{}",attributeAddNewDTO);
        attributeService.addNew(attributeAddNewDTO);
        return JsonResult.ok();
    }


    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除属性")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAnyAuthority('/pms/product/delete','/pms/product/read')")
    public JsonResult deleteById(@PathVariable Long id){
        log.debug("准备开始处理删除属性业务，接受到的参数为：{}",id);
        attributeService.deleteById(id);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("属性列表")
    @ApiOperationSupport(order = 400)
    @PreAuthorize("hasAnyAuthority('/pms/product/delete','/pms/product/read')")
    public JsonResult list(){
        log.debug("开始处理查询属性列表业务");
        List<AttributeListItemVO> list = attributeService.getAttributes();
        return JsonResult.ok(list);

    }
}
